#ifndef __VTOR_MODBUS_H__
#define __VTOR_MODBUS_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "vtor_elec_module_config.h"
#include "vtor_modbus_datatype.h"

#ifdef __VTOR_MODBUS__

#define VTOR_MODBUS_RW_REG_START_ADDR		40001
#define VTOR_MODBUS_RW_REG_END_ADDR			49999
	
#define VTOR_MODBUS_RO_REG_START_ADDR		30001
#define VTOR_MODBUS_RO_REG_END_ADDR			39999
	
typedef struct VtorModbusRegister
{
	uint16_t addr;
	uint16_t value;
}VtorModbusRegStruct;

extern uint8_t vtorModbusId;
extern VtorModbusRegStruct* vtorModbusRegisterHeader;


// modbus基本操作，校验，读写寄存器等
uint8_t VtorModbus_CheckCrc(uint8_t* buf, uint16_t totalLen);
struct VtorModbusRegister* VtorModbus_SearchAddress(uint16_t addr);
uint8_t VtorModbus_ReadReg(uint16_t regAddr, uint16_t* regBuf, uint16_t regNum);
uint8_t VtorModbus_WriteReg(uint16_t regAddr, uint16_t* regBuf, uint16_t regNum);

// modbusslave相关操作
uint16_t VtorModbusSlave_Process(uint8_t* rxBuf, uint16_t len);

// modbusmaster相关操作
uint16_t VtorModbusMaster_Process(uint8_t* rxBuf, uint16_t len);
uint8_t VtorModbus_ReadSlaveReg(uint8_t slaveId, uint16_t regAddr, uint16_t* regBuf, uint16_t regNum);
uint8_t VtorModbus_WriteSlaveReg(uint8_t slaveId, uint16_t regAddr, uint16_t* regBuf, uint16_t regNum);

// 用户自行实现接口
void VtorModbus_Init(uint8_t id);
void VtorModbus_SendBuffer(uint8_t* buf, uint16_t len);

#endif // __VTOR_MODBUS__

#ifdef __cplusplus
}
#endif

#endif

